<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>引用类型 | 我的世界</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/notes/favicon.ico">
    <link rel="manifest" href="/notes/manifest.json">
    <meta name="description" content="学习，生活，还有美食~">
    
    <link rel="preload" href="/notes/assets/css/0.styles.8453c794.css" as="style"><link rel="preload" href="/notes/assets/js/app.f19b5e39.js" as="script"><link rel="preload" href="/notes/assets/js/2.69392339.js" as="script"><link rel="preload" href="/notes/assets/js/45.b89cd059.js" as="script"><link rel="preload" href="/notes/assets/js/14.e376f3f7.js" as="script"><link rel="prefetch" href="/notes/assets/js/10.4748ef2e.js"><link rel="prefetch" href="/notes/assets/js/11.4ec67c3b.js"><link rel="prefetch" href="/notes/assets/js/12.db234a0a.js"><link rel="prefetch" href="/notes/assets/js/13.0bdeeeea.js"><link rel="prefetch" href="/notes/assets/js/15.873f63d0.js"><link rel="prefetch" href="/notes/assets/js/16.6d650151.js"><link rel="prefetch" href="/notes/assets/js/17.f0bcb5c9.js"><link rel="prefetch" href="/notes/assets/js/18.7dffee3a.js"><link rel="prefetch" href="/notes/assets/js/19.5f0252c4.js"><link rel="prefetch" href="/notes/assets/js/20.abea2681.js"><link rel="prefetch" href="/notes/assets/js/21.252713aa.js"><link rel="prefetch" href="/notes/assets/js/22.c99488d0.js"><link rel="prefetch" href="/notes/assets/js/23.3839292f.js"><link rel="prefetch" href="/notes/assets/js/24.9cc2f325.js"><link rel="prefetch" href="/notes/assets/js/25.e9dcd2f1.js"><link rel="prefetch" href="/notes/assets/js/26.31898ae6.js"><link rel="prefetch" href="/notes/assets/js/27.88db2371.js"><link rel="prefetch" href="/notes/assets/js/28.2d29c56c.js"><link rel="prefetch" href="/notes/assets/js/29.a0b42251.js"><link rel="prefetch" href="/notes/assets/js/3.48b2b659.js"><link rel="prefetch" href="/notes/assets/js/30.9b38a2bb.js"><link rel="prefetch" href="/notes/assets/js/31.d374da8e.js"><link rel="prefetch" href="/notes/assets/js/32.7868c3f3.js"><link rel="prefetch" href="/notes/assets/js/33.87730e15.js"><link rel="prefetch" href="/notes/assets/js/34.5a5b6c6e.js"><link rel="prefetch" href="/notes/assets/js/35.1deedbd4.js"><link rel="prefetch" href="/notes/assets/js/36.d39f2b24.js"><link rel="prefetch" href="/notes/assets/js/37.d87637b4.js"><link rel="prefetch" href="/notes/assets/js/38.d118907c.js"><link rel="prefetch" href="/notes/assets/js/39.9f2c8514.js"><link rel="prefetch" href="/notes/assets/js/4.161aee82.js"><link rel="prefetch" href="/notes/assets/js/40.917feb30.js"><link rel="prefetch" href="/notes/assets/js/41.78e1b969.js"><link rel="prefetch" href="/notes/assets/js/42.cd6ac9d4.js"><link rel="prefetch" href="/notes/assets/js/43.961a438d.js"><link rel="prefetch" href="/notes/assets/js/44.ef1661d7.js"><link rel="prefetch" href="/notes/assets/js/46.8ca9e560.js"><link rel="prefetch" href="/notes/assets/js/47.a35bec74.js"><link rel="prefetch" href="/notes/assets/js/48.953bb15e.js"><link rel="prefetch" href="/notes/assets/js/49.ae614c87.js"><link rel="prefetch" href="/notes/assets/js/5.ef4783db.js"><link rel="prefetch" href="/notes/assets/js/50.a9fbc190.js"><link rel="prefetch" href="/notes/assets/js/51.12ae367b.js"><link rel="prefetch" href="/notes/assets/js/52.57a5efe4.js"><link rel="prefetch" href="/notes/assets/js/53.861eb65b.js"><link rel="prefetch" href="/notes/assets/js/54.3bc1c9a1.js"><link rel="prefetch" href="/notes/assets/js/55.f724b26c.js"><link rel="prefetch" href="/notes/assets/js/56.02e48e7a.js"><link rel="prefetch" href="/notes/assets/js/6.72ed1ea6.js"><link rel="prefetch" href="/notes/assets/js/7.f1e39dec.js"><link rel="prefetch" href="/notes/assets/js/8.84b75613.js"><link rel="prefetch" href="/notes/assets/js/9.e1cd5390.js">
    <link rel="stylesheet" href="/notes/assets/css/0.styles.8453c794.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/notes/" class="home-link router-link-active"><img src="/notes/images/fish.gif" alt="我的世界" class="logo"> <span class="site-name can-hide">我的世界</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/notes/notes/" class="nav-link router-link-active">🎓 学习</a></div><div class="nav-item"><a href="/notes/life/" class="nav-link">🏸 生活</a></div> <a href="https://gitee.com/zgj6" target="_blank" rel="noopener noreferrer" class="repo-link">
    git
    <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p title="学习笔记" class="sidebar-heading"><span>学习笔记</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="JavaScript 高程笔记" class="sidebar-heading open"><span>JavaScript 高程笔记</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/notes/notes/pro-js-for-web-dev-note/" aria-current="page" title="JavaScript 高级程序设计 · 笔记" class="sidebar-link">JavaScript 高级程序设计 · 笔记</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter01.html" title="第一章 JavaScript简介" class="sidebar-link">第一章 JavaScript简介</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter02.html" title="第二章 在HTML中使用JavaScript" class="sidebar-link">第二章 在HTML中使用JavaScript</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter03.html" title="第3章 基本概念" class="sidebar-link">第3章 基本概念</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter04.html" title="第四章 变量、作用域和内存问题" class="sidebar-link">第四章 变量、作用域和内存问题</a></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html" aria-current="page" title="引用类型" class="active sidebar-link">引用类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#object类型" title="Object类型" class="sidebar-link">Object类型</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#array类型" title="Array类型" class="sidebar-link">Array类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#检测数组" title="检测数组" class="sidebar-link">检测数组</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#转换方法" title="转换方法" class="sidebar-link">转换方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#栈方法" title="栈方法" class="sidebar-link">栈方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#队列方法" title="队列方法" class="sidebar-link">队列方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#重排序方法" title="重排序方法" class="sidebar-link">重排序方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#操作方法" title="操作方法" class="sidebar-link">操作方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#位置方法" title="位置方法" class="sidebar-link">位置方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#迭代方法" title="迭代方法" class="sidebar-link">迭代方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#归并方法" title="归并方法" class="sidebar-link">归并方法</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#date类型" title="Date类型" class="sidebar-link">Date类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#继承的方法" title="继承的方法" class="sidebar-link">继承的方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#日期格式化方法" title="日期格式化方法" class="sidebar-link">日期格式化方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#日期-时间组件方法" title="日期/时间组件方法" class="sidebar-link">日期/时间组件方法</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#regexp类型" title="RegExp类型" class="sidebar-link">RegExp类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#实例属性" title="实例属性" class="sidebar-link">实例属性</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#regexp实例方法" title="RegExp实例方法" class="sidebar-link">RegExp实例方法</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#function-类型" title="Function 类型" class="sidebar-link">Function 类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#没有重载" title="没有重载" class="sidebar-link">没有重载</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#作为值的函数" title="作为值的函数" class="sidebar-link">作为值的函数</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#函数内部的属性" title="函数内部的属性" class="sidebar-link">函数内部的属性</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#函数的属性和方法" title="函数的属性和方法" class="sidebar-link">函数的属性和方法</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#基本包装类型" title="基本包装类型" class="sidebar-link">基本包装类型</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#slice" title="slice()" class="sidebar-link">slice()</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#substring" title="substring()" class="sidebar-link">substring()</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#substr" title="substr()" class="sidebar-link">substr()</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#trim" title="trim()" class="sidebar-link">trim()</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#字符串大小写转换方法" title="字符串大小写转换方法" class="sidebar-link">字符串大小写转换方法</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#match" title="match()" class="sidebar-link">match()</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#replace" title="replace()" class="sidebar-link">replace()</a></li></ul></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#单体内置对象" title="单体内置对象" class="sidebar-link">单体内置对象</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#global对象" title="Global对象" class="sidebar-link">Global对象</a></li><li class="sidebar-sub-header"><a href="/notes/notes/pro-js-for-web-dev-note/chapter05.html#math对象" title="Math对象" class="sidebar-link">Math对象</a></li></ul></li></ul></li><li><a href="/notes/notes/pro-js-for-web-dev-note/chapter06.html" title="面向对象的程序设计" class="sidebar-link">面向对象的程序设计</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p title="HTTP" class="sidebar-heading"><span>HTTP</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="微信小程序" class="sidebar-heading"><span>微信小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="TS" class="sidebar-heading"><span>TS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="Vite" class="sidebar-heading"><span>Vite</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="interviewquestion" class="sidebar-heading"><span>interviewquestion</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="AI" class="sidebar-heading"><span>AI</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p title="UE" class="sidebar-heading"><span>UE</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="引用类型"><a href="#引用类型" class="header-anchor">#</a> 引用类型</h1> <h2 id="object类型"><a href="#object类型" class="header-anchor">#</a> Object类型</h2> <p>引用类型的值（<strong>对象</strong>）是引用类型的一个实例。</p> <p>尽管 ECMAScript从技术上讲是一门面向对象的语言，但它不具备传统的面向对象语言所支持的<strong>类</strong>和<strong>接口</strong>等基本结构。</p> <p>创建Object实例的方式有两种：new操作符后跟Object构造函数、<strong>对象字面量</strong>表示法。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Object</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
person<span class="token punctuation">.</span>name <span class="token operator">=</span> <span class="token string">&quot;YourName&quot;</span><span class="token punctuation">;</span>
person<span class="token punctuation">.</span>age <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> person1 <span class="token operator">=</span> <span class="token punctuation">{</span>
    name<span class="token operator">:</span> <span class="token string">&quot;YourName&quot;</span><span class="token punctuation">,</span>
    age<span class="token operator">:</span> <span class="token keyword">null</span>
<span class="token punctuation">}</span>

<span class="token comment">// 如果在最后一个属性后面添加逗号，会在 IE7 及更早版本和Opera 中导致错误</span>

<span class="token keyword">var</span> person2 <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span> <span class="token comment">//与 new Object()相同</span>
</code></pre></div><ul><li>在通过对象字面量定义对象时，实际上不会调用 Object 构造函数。</li> <li>开发人员更青睐对象字面量语法。</li> <li>对象字面量也是向函数传递大量可选参数的首选方式</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">displayInfo</span><span class="token punctuation">(</span><span class="token parameter">args</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> output <span class="token operator">=</span> <span class="token string">&quot;&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> args<span class="token punctuation">.</span>name <span class="token operator">==</span> <span class="token string">&quot;string&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        output <span class="token operator">+=</span> <span class="token string">&quot;Name: &quot;</span> <span class="token operator">+</span> args<span class="token punctuation">.</span>name <span class="token operator">+</span> <span class="token string">&quot;\n&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">typeof</span> args<span class="token punctuation">.</span>age <span class="token operator">==</span> <span class="token string">&quot;number&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        output <span class="token operator">+=</span> <span class="token string">&quot;Age: &quot;</span> <span class="token operator">+</span> args<span class="token punctuation">.</span>age <span class="token operator">+</span> <span class="token string">&quot;\n&quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">alert</span><span class="token punctuation">(</span>output<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">// 对象字面量也是向函数传递大量可选参数的首选方式</span>
<span class="token function">displayInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    name<span class="token operator">:</span> <span class="token string">&quot;Nicholas&quot;</span><span class="token punctuation">,</span>
    age<span class="token operator">:</span> <span class="token number">29</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">displayInfo</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
    name<span class="token operator">:</span> <span class="token string">&quot;Greg&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>放问对象属性可以用<strong>点表示法</strong>，亦可以用<strong>方括号表示法</strong>，在使用方括号语法时，应该将要访问的属性
以字符串的形式放在方括号中。</p> <div class="language-js extra-class"><pre class="language-js"><code>person<span class="token punctuation">[</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">]</span>
person<span class="token punctuation">.</span>name
</code></pre></div><p>这两种访问对象属性的方法没有什么区别，但方括号访问法的主要优点是可以通过变量来访问属性。</p> <p>如果属性名中包含会导致语法错误的字符（如<code>person[&quot;first name&quot;]</code>），或者属性名使用的是关键字或保留字，也可以使用方括
号表示法。</p> <p>除非必须使用变量来访问属性，否则我们建议使用点表示法。</p> <h2 id="array类型"><a href="#array类型" class="header-anchor">#</a> Array类型</h2> <p>ECMAScript数组的每一项可以保存任何类型的数据，数组的大小是可以动态调整的。</p> <p>创建数组的方式：使用Array构造函数、数组字面量表示法。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr1 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr3 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Array</span><span class="token punctuation">(</span><span class="token string">'aaa'</span><span class="token punctuation">,</span><span class="token string">'bbb'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr4 <span class="token operator">=</span> <span class="token function">Array</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> arr5 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr6 <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
</code></pre></div><p>在使用数组字面量表示法时，也不会调用Array构造函数。</p> <p>数组的读取和设置数组值，使用例如<code>arr[0]</code>，0是第一个数组的索引。</p> <p>length 属性很有特点——它不是只读的。因此，通过设置这个属性，可以从数组的末尾移
除项或向数组中添加新项。</p> <h3 id="检测数组"><a href="#检测数组" class="header-anchor">#</a> 检测数组</h3> <p>检测数组有两种方式：<code>instanceof</code>和<code>Array.isArray()</code>，IE8不支持后一种方法。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr <span class="token keyword">instanceof</span> <span class="token class-name">Array</span><span class="token punctuation">;</span>   <span class="token comment">// true</span>
Array<span class="token punctuation">.</span><span class="token function">isArray</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// true</span>
</code></pre></div><h3 id="转换方法"><a href="#转换方法" class="header-anchor">#</a> 转换方法</h3> <p>所有对象都有<code>toLocaleString()</code>、<code>toString()</code>、<code>valueOf</code>方法。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'red'</span><span class="token punctuation">,</span> <span class="token string">'green'</span><span class="token punctuation">,</span> <span class="token string">'blue'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">toString</span><span class="token punctuation">(</span><span class="token punctuation">)</span>  <span class="token comment">// &quot;red,green,blue&quot;</span>
arr<span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
</code></pre></div><p>使用<code>join()</code>方法，可以使用不同的分隔符来构建这个字符串。</p> <div class="language-js extra-class"><pre class="language-js"><code>arr<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'|'</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;red|green|blue&quot;</span>
arr<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;red/green/blue&quot;</span>
arr<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">' '</span><span class="token punctuation">)</span>   <span class="token comment">// &quot;red green blue&quot;</span>
</code></pre></div><h3 id="栈方法"><a href="#栈方法" class="header-anchor">#</a> 栈方法</h3> <p>栈是一种后进先出（LIFO）的数据结构。</p> <p>push()方法可以接收任意数量的参数，把它们逐个添加到数组末尾，并返回修改后数组的长度。</p> <p>pop()方法不接收参数，从数组末尾移除最后一项，减少数组的 length 值，然后返回移除的项。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'red'</span><span class="token punctuation">,</span> <span class="token string">'green'</span><span class="token punctuation">,</span> <span class="token string">'blue'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'yellow'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// 返回 4， 数组内容：[ &quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot; ]</span>
arr<span class="token punctuation">.</span><span class="token function">pop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">// 返回 &quot;yellow&quot;，数组内容：[ &quot;red&quot;, &quot;green&quot;, &quot;blue&quot; ]</span>
</code></pre></div><h3 id="队列方法"><a href="#队列方法" class="header-anchor">#</a> 队列方法</h3> <p>队列是一种先进先出（FIFO）的数据结构。</p> <p><code>shift()</code>能够移除数组中的第一个项并返回该项，同时将数组长度减 1。</p> <p>结合<code>push()</code>方法使用。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'red'</span><span class="token punctuation">,</span> <span class="token string">'green'</span><span class="token punctuation">,</span> <span class="token string">'blue'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span><span class="token string">'umi'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// 4</span>
arr<span class="token punctuation">.</span><span class="token function">shift</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">// &quot;red&quot;</span>
</code></pre></div><p><code>unshift()</code>与<code>shift()</code>的用途相反：它能在数组前端添加任意个项并返回新数组的长度。</p> <h3 id="重排序方法"><a href="#重排序方法" class="header-anchor">#</a> 重排序方法</h3> <p><code>reverse()</code>方法翻转数组项的顺序。
<code>sort()</code>方法按升序排列数组项，即由小到大排列，会改变原来的值。</p> <p>sort()方法会调用每个数组项的 toString()转型方法，然后比较得到的<strong>字符串</strong>，以确定如何排序。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> val <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token number">15</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
val<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// [ 0, 1, 10, 15, 5 ]</span>

<span class="token comment">// 这种排序是有问题的，因此 sort()方法可以接收一个比较函数作为参数，</span>
<span class="token comment">// 以便我们指定哪个值位于哪个值的前面。</span>

<span class="token keyword">function</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>n1<span class="token operator">&gt;</span>n2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>n1<span class="token operator">&lt;</span>n2<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
val<span class="token punctuation">.</span><span class="token function">sort</span><span class="token punctuation">(</span>compare<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// [ 0, 1, 5, 10, 15 ]</span>
</code></pre></div><p>compare函数一种更简单的写法</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">compare</span><span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> n1<span class="token operator">-</span>n2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h3 id="操作方法"><a href="#操作方法" class="header-anchor">#</a> 操作方法</h3> <p><code>concat()</code>方法：基于当前数组的所有项创建一个新数组。这个方法会先创建当前数组一个副本，然后将接收到的参数
添加到这个副本的末尾，最后返回新构建的数组。如果传递给 concat()方法的是一或多个数组，则该方法会将这些数组中的
每一项都添加到结果数组中。如果传递的值不是数组，这些值就会被简单地添加到结果数组的末尾。不会改变原数组。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">'red'</span><span class="token punctuation">,</span><span class="token string">'green'</span><span class="token punctuation">,</span><span class="token string">'blue'</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr2 <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">concat</span><span class="token punctuation">(</span><span class="token string">'yellow'</span><span class="token punctuation">,</span><span class="token punctuation">[</span><span class="token string">'black'</span><span class="token punctuation">,</span><span class="token string">'brown'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span>   <span class="token comment">// [ &quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;, &quot;black&quot;, &quot;brown&quot; ]</span>
</code></pre></div><p><code>slice()</code>方法能够基于当前数组中的一个或多个创建一个新的数组。接收2个参数：起始值和结束值。在只有一个参数的情况下，返回从指定位置到数组末尾的所有项。如果有两个参数，该方法返回起始到结束位置之间的项——但不包括结束位置的项。如果参数中有负数，则用数组长度加上这个负数。不会改变原数组。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;yellow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;brown&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr1 <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> arr2 <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr1<span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// [ &quot;green&quot;, &quot;blue&quot; ]</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr2<span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// [ &quot;black&quot;, &quot;brown&quot; ]</span>
</code></pre></div><p><code>splice()</code>有多种用法，主要用途是向数组中部插入项。会改变原数组。</p> <ul><li>删除：只需指定2个参数：需要删除的第一项位置和需要删除的项数。例如<code>splice(0,2)</code>删除数组中的前两项。</li> <li>插入：指定3个或以上参数：起始位置、0（要删除的项数）、要插入的项。<code>splice(2,0,'red','green')</code>。</li> <li>替换：指定3个或以上参数：起始位置、要删除的项数和要插入的任意数量的项。<code>splice(2,1,&quot;red&quot;,&quot;green&quot;)</code>。</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string">&quot;red&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;green&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;blue&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;yellow&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;black&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;brown&quot;</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>        <span class="token comment">// 删除从0开始的2个字符串</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>       <span class="token comment">// [ &quot;blue&quot;, &quot;yellow&quot;, &quot;black&quot;, &quot;brown&quot; ]</span>
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token string">'red'</span><span class="token punctuation">,</span><span class="token string">'green'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// 从第0项插入2个字符串</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>                   <span class="token comment">// [ &quot;red&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;, &quot;black&quot;, &quot;brown&quot; ]</span>
arr<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token string">'reddit'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 替换第0个字符串内容</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>arr<span class="token punctuation">)</span><span class="token punctuation">;</span>           <span class="token comment">// [ &quot;reddit&quot;, &quot;green&quot;, &quot;blue&quot;, &quot;yellow&quot;, &quot;black&quot;, &quot;brown&quot; ]</span>
</code></pre></div><p>splice()方法始终都会返回一个数组，该数组中包含从原始数组中删除的项</p> <h3 id="位置方法"><a href="#位置方法" class="header-anchor">#</a> 位置方法</h3> <p>ES5中增加了两个位置方法：<code>indexOf()</code>和<code>lastIndexOf()</code>，都接收2个参数要查找的项目和表示查找起始位置的索引（可选）。这两种数组方法同样支持字符串。
前者从数组开头查找，后者从数组末尾开始查找，起始查找位置<strong>永远都是从0开始</strong>。在查找时都使用全等操作符，在没有找到时返回-1。</p> <p>这两个方法都可以接收可选的第二个参数，表示从字符串中的哪个位置开始搜索。indexOf()会从该参数指定的位置向后搜索，忽略该位置之前的所有字符；而 lastIndexOf()则会从指定的位置向前搜索，忽略该位置之后的所有字符。第二个参数为可选参数，它的合法取值是 0 到 stringObject.length - 1。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">// 3</span>
arr<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 8</span>
arr<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 8</span>
arr<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token comment">// 3</span>
arr<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token comment">// -1，说明没有找到</span>
<span class="token keyword">var</span> person <span class="token operator">=</span> <span class="token punctuation">{</span>name<span class="token operator">:</span><span class="token string">'urname'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> people <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">{</span>name<span class="token operator">:</span><span class="token string">'urname'</span><span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> mPeople <span class="token operator">=</span> <span class="token punctuation">[</span>person<span class="token punctuation">]</span><span class="token punctuation">;</span>
people<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span>  <span class="token comment">// -1</span>
mPeople<span class="token punctuation">.</span><span class="token function">indexOf</span><span class="token punctuation">(</span>person<span class="token punctuation">)</span> <span class="token comment">// 0</span>
</code></pre></div><p>自创应用题：一个链接地址<code>https://www.example.com/test?arg1=123&amp;argb=456</code>，用上述方法获取其域名和参数。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> link <span class="token operator">=</span> <span class="token string">'https://www.example.com/test?arg1=123&amp;argb=456'</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> domain <span class="token operator">=</span> link<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>link<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
domain<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>domain<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'/'</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
domain <span class="token operator">=</span> domain<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>domain<span class="token punctuation">)</span>         <span class="token comment">// www.example.com</span>

<span class="token keyword">var</span> temp <span class="token operator">=</span> link<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
temp<span class="token punctuation">.</span><span class="token function">splice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span>link<span class="token punctuation">.</span><span class="token function">lastIndexOf</span><span class="token punctuation">(</span><span class="token string">'?'</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> attrString <span class="token operator">=</span> temp<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>attrString<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// arg1=123&amp;argb=456</span>

<span class="token keyword">var</span> attr <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> args <span class="token operator">=</span> attrString<span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'&amp;'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>args<span class="token punctuation">.</span>length<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">var</span> temp <span class="token operator">=</span> args<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string">'='</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  attr<span class="token punctuation">[</span>temp<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>attr<span class="token punctuation">)</span><span class="token punctuation">;</span>      <span class="token comment">// Object { arg1: &quot;123&quot;, argb: &quot;456&quot; }</span>
</code></pre></div><h3 id="迭代方法"><a href="#迭代方法" class="header-anchor">#</a> 迭代方法</h3> <p>ES5提供了5个数组迭代方法。传入这些方法中的函数会接收三个参数：数
组项的值、该项在数组中的位置和数组对象本身。这些方法不会修改数组中的包含的值。</p> <p>使用方法：<code>arr.every(function(item,index,array){})</code>，里面的函数会对数组的每一项运行，运行该函数的作用域对象影响 this 的值。</p> <ul><li><code>every()</code>：如果该函数对<strong>每一项</strong>都返回true，则返回true，否则返回false。</li> <li><code>filter()</code>：函数会返回<strong>有true的项</strong>组成的数组。</li> <li><code>forEach()</code>：仅仅运行函数，没有返回值。</li> <li><code>map()</code>：返回每次函数调用的<strong>结果</strong>返回的数组。</li> <li><code>some()</code>：如果该函数对<strong>任一项</strong>返回true，则返回true。</li></ul> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>

<span class="token keyword">var</span> everyResult <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">every</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index<span class="token punctuation">,</span>array</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>everyResult<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// false</span>

<span class="token keyword">var</span> someResult <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">some</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span>index<span class="token punctuation">,</span>array</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>someResult<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// ture</span>

<span class="token keyword">var</span> filterResult <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">filter</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token punctuation">(</span>item <span class="token operator">&gt;</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>filterResult<span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// [3,4,5,4,3]</span>

<span class="token keyword">var</span> mapResult <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">item<span class="token punctuation">,</span> index<span class="token punctuation">,</span> array</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> item<span class="token operator">*</span><span class="token number">2</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>mapResult<span class="token punctuation">)</span>  <span class="token comment">// [ 2, 4, 6, 8, 10, 8, 6, 4, 2 ]</span>
</code></pre></div><h3 id="归并方法"><a href="#归并方法" class="header-anchor">#</a> 归并方法</h3> <p>ES5还增加了两个归并数组的方法。这两个方法都会迭代数组的所有项，然后构建一个最终返回的值。<code>reduce()</code>方法从数组的第一项开始，逐个遍历到最后。而<code>reduceRight()</code>则从数组的最后一项开始，向前遍历到第一项。这两个方法都接收两个参数：一个在每一项上调用的函数和（可选的）作为归并基础的初始值。其内部的函数接收4个参数：前一个值、当前值、项的索引和数组对象。这个函数返回的任何值都会作为第一个参数自动传给下一项。</p> <p>使用 reduce()还是 reduceRight()，主要取决于要从哪头开始遍历数组。除此之外，它们完全相同。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 用 reduce() 方法求和</span>
<span class="token keyword">var</span> arr <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token number">4</span><span class="token punctuation">,</span><span class="token number">3</span><span class="token punctuation">,</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result <span class="token operator">=</span> arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">(</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">prev<span class="token punctuation">,</span>cur<span class="token punctuation">,</span>index<span class="token punctuation">,</span>array</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token punctuation">[</span>prev<span class="token punctuation">,</span>cur<span class="token punctuation">,</span>index<span class="token punctuation">,</span>array<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">return</span> prev<span class="token operator">+</span>cur<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// 25</span>
</code></pre></div><h2 id="date类型"><a href="#date类型" class="header-anchor">#</a> Date类型</h2> <p>Date类型基于早期Java中的Date基础构建，使用UTC与1970年1月1日 零时开始经过的毫秒数来纪录时间。</p> <p>创建一个Date对象：<code>var now = new Date();</code></p> <p>在调用 Date 构造函数而不传递参数的情况下，新创建的对象自动获得当前日期和时间。</p> <p>如果想指定Date()日期时间，必须向构造函数传递毫秒数，ES提供了两个方法：<code>Date.parse()</code>和<code>Date.UTC()</code>。</p> <p>Date.parse()这个方法没有定义日期格式标准，因此实现方式因区域各异。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> someDate <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>Date<span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token string">&quot;May 25, 2004&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> someDate <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token string">&quot;May 25, 2004&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">// 这两种写法相同</span>
</code></pre></div><p>Date.UTC()的参数分别是年份、基于 0 的月份（一月是 0，二月是 1，以此类推）、月中的哪一天
（1 到 31）、小时数（0 到 23）、分钟、秒以及毫秒数。在这些参数中，只有前两个参数（年和月）是必
需的。</p> <p>Date 构造函数也会模仿 Date.UTC()，但有一点明显不同：日期和时间都基于本地时区而非 GMT 来创建。
不过，Date 构造函数接收的参数仍然与 Date.UTC()相同。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// GMT 时间 2000 年 1 月 1 日午夜零时</span>
<span class="token keyword">var</span> y2k <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>Date<span class="token punctuation">.</span><span class="token constant">UTC</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// GMT 时间 2005 年 5 月 5 日下午 5:55:55</span>
<span class="token keyword">var</span> allFives <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>Date<span class="token punctuation">.</span><span class="token constant">UTC</span><span class="token punctuation">(</span><span class="token number">2005</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 本地时间 2000 年 1 月 1 日午夜零时</span>
<span class="token keyword">var</span> y2k <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2000</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 本地时间 2005 年 5 月 5 日下午 5:55:55</span>
<span class="token keyword">var</span> allFives <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token number">2005</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">17</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>ES5添加了<code>Data.now()</code>方法，返回表示调用这个方法时的日期和时间的毫秒数。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> start <span class="token operator">=</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">var</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span><span class="token number">999</span><span class="token operator">*</span><span class="token number">100</span><span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> end <span class="token operator">=</span> Date<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> result <span class="token operator">=</span> end <span class="token operator">-</span> start<span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'时间差：'</span><span class="token operator">+</span> result <span class="token operator">+</span><span class="token string">' 毫秒'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">// 时间差：76 毫秒</span>
</code></pre></div><h3 id="继承的方法"><a href="#继承的方法" class="header-anchor">#</a> 继承的方法</h3> <p>Date 类型也重写了 toLocaleString()、toString()和 valueOf()。 toLocaleString()方法会按照与浏览器设置的地区相适应的格式返回日期和时间。 事实上，toLocaleString()和 toString()的这一差别仅在调试代码时比较有用，而在显示日期和时间时没有什么价值。</p> <h3 id="日期格式化方法"><a href="#日期格式化方法" class="header-anchor">#</a> 日期格式化方法</h3> <ul><li>toDateString()——以特定于实现的格式显示星期几、月、日和年；</li> <li>toTimeString()——以特定于实现的格式显示时、分、秒和时区；</li> <li>toLocaleDateString()——以特定于地区的格式显示星期几、月、日和年；</li> <li>toLocaleTimeString()——以特定于实现的格式显示时、分、秒；</li> <li>toUTCString()——以特定于实现的格式完整的 UTC 日期。</li></ul> <h3 id="日期-时间组件方法"><a href="#日期-时间组件方法" class="header-anchor">#</a> 日期/时间组件方法</h3> <p>提供了很多的方法，去看文档。</p> <h2 id="regexp类型"><a href="#regexp类型" class="header-anchor">#</a> RegExp类型</h2> <p>ES通过RegExp类型支持正则表达式。写法：<code>var reg = /pattern/flags</code>。其中pattern指的是任何简单或复杂的正则表达式，flags指的是标志，可以带一个或多个标志以标明正则表达式的行为。</p> <p>flags:</p> <ul><li><code>g</code> 全局模式，模式将被应用于所有字符，而非发现第一个匹配项时立即停止。</li> <li><code>i</code> 不区分大小写模式。</li> <li><code>m</code> 多行模式，到达一行文本末尾时还会继续查找下一行中是否存在与模式匹配的项。</li></ul> <p>模式中的<strong>元字符</strong>必须转义（使用转义符<code>\</code>），这些元字符有：<code>( [ { } ] ) + * \ | . ^ $ ?</code></p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 以字面量形式定义正则表达式：匹配第一个“bat”或“cat”，不区分大小写。</span>
<span class="token keyword">var</span> reg1 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[bc]at</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>

<span class="token comment">// 以构造函数方式定义正则表达式，与reg1作用相同。</span>
<span class="token keyword">var</span> reg2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegExp</span><span class="token punctuation">(</span><span class="token string">&quot;[bc]at&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;i&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 使用转义符，匹配第一个“\[bc\]at”，不区分大小写。</span>
<span class="token keyword">var</span> reg3 <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\[bc\]at</span><span class="token regex-delimiter">/</span><span class="token regex-flags">i</span></span><span class="token punctuation">;</span>
</code></pre></div><h3 id="实例属性"><a href="#实例属性" class="header-anchor">#</a> 实例属性</h3> <p>RegExp的每个实例都有以下属性：<code>global, ignoreCase, lastIndex, multiline, source</code>，可以用<code>.</code>来调用查看。</p> <h3 id="regexp实例方法"><a href="#regexp实例方法" class="header-anchor">#</a> RegExp实例方法</h3> <p>主要方法<code>exec()</code>，返回值包含了匹配信息的数组和两个额外属性：<code>index</code>和<code>input</code>。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token comment">// 在没有捕获组的情况下，只返回包含第一个匹配项信息的数组。</span>
<span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">'batcatnatisstring'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> reg <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">[cb]at</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>reg<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 在有捕获组的情况下，第一项是与整个模式匹配的字符串，其他项是与模式中的捕获组匹配的字符串</span>
<span class="token keyword">var</span> text <span class="token operator">=</span> <span class="token string">&quot;mom and dad and baby&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">mom( and dad( and baby)?)?</span><span class="token regex-delimiter">/</span><span class="token regex-flags">gi</span></span><span class="token punctuation">;</span>   <span class="token comment">// 有两个捕获组</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>pattern<span class="token punctuation">.</span><span class="token function">exec</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span>
</code></pre></div><p>对于 exec()方法而言，即使在模式中设置了全局标志（g），它每次也只会返回一个匹配项。在不
设置全局标志的情况下，在同一个字符串上多次调用 exec()将始终返回第一个匹配项的信息。而在设
置全局标志的情况下，每次调用 exec()则都会在字符串中继续查找新匹配项，在全局模式下lastIndex的值在每次调用
exec()后都会增加。</p> <p><code>test()</code>方法，它接收一个字符串参数。在模式与改参数匹配的情况下返回true否则返回false。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> text <span class="token operator">=</span> <span class="token string">&quot;000-00-0000&quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> pattern <span class="token operator">=</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\d{3}-\d{2}-\d{4}</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">;</span>

<span class="token keyword">if</span> <span class="token punctuation">(</span>pattern<span class="token punctuation">.</span><span class="token function">test</span><span class="token punctuation">(</span>text<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token function">alert</span><span class="token punctuation">(</span><span class="token string">&quot;The pattern was matched.&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><h2 id="function-类型"><a href="#function-类型" class="header-anchor">#</a> Function 类型</h2> <p>函数实际上是对象。每个函数都是Function类型的实例，而且与其他类型一样具有属性和方法。函数名实际上是指向函数对象的指针。</p> <p>函数声明语法：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">sum</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span> n2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> n1<span class="token operator">+</span>n2<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> <span class="token function-variable function">sum1</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">n1<span class="token punctuation">,</span>n2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> n1<span class="token operator">+</span>n2<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token comment">// 使用Function构造函数，不推荐</span>
<span class="token keyword">var</span> sum2 <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Function</span><span class="token punctuation">(</span><span class="token string">&quot;n1&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;n2&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;return n1+n2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>函数名仅仅是指向函数的指针，所以一个函数可以有多个名字。使用不带圆括号的的函数名是访问函数指针，而非调用函数。</p> <h3 id="没有重载"><a href="#没有重载" class="header-anchor">#</a> 没有重载</h3> <p>如果声明了2个同名函数，那么后面的函数会覆盖前面的的函数。</p> <p>以下代码之所以会在运行期间产生错误，原因在于函数位于一个初始化语句中，而不是一个函数声
明。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token function">alert</span><span class="token punctuation">(</span><span class="token function">sum</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">// TypeError: sum is not a function</span>
<span class="token keyword">var</span> <span class="token function-variable function">sum</span> <span class="token operator">=</span> <span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">num1<span class="token punctuation">,</span> num2</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
    <span class="token keyword">return</span> num1 <span class="token operator">+</span> num2<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
</code></pre></div><h3 id="作为值的函数"><a href="#作为值的函数" class="header-anchor">#</a> 作为值的函数</h3> <p>你不仅可以像传递参数一样把函数传给另一个函数，而且可以将一个函数作为另一个函数的结果返回。</p> <h3 id="函数内部的属性"><a href="#函数内部的属性" class="header-anchor">#</a> 函数内部的属性</h3> <p>函数内部有两个属性：<code>arguments</code>和<code>this</code>。argument中还有一个属性<code>callee</code>，用于指向这个函数本身，可用于递归调用。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">factorical</span><span class="token punctuation">(</span><span class="token parameter">num</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span>num <span class="token operator">&lt;=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token number">1</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> num <span class="token operator">*</span> arguments<span class="token punctuation">.</span><span class="token function">callee</span><span class="token punctuation">(</span>num<span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre></div><p>this引用的是函数据以执行的环境对象。当在网页的全局作用域中调用函数时，this 对象引用的就是 window</p> <p>函数被谁调用时，this就指向谁</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> color <span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>color<span class="token operator">:</span><span class="token string">'blue'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// red; this是window</span>

obj<span class="token punctuation">.</span>fn <span class="token operator">=</span> fn<span class="token punctuation">;</span>
obj<span class="token punctuation">.</span><span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// blue, this是obj</span>
</code></pre></div><h3 id="函数的属性和方法"><a href="#函数的属性和方法" class="header-anchor">#</a> 函数的属性和方法</h3> <p>每个函数都包含两个属性：<code>length</code>和<code>prototype</code>。length属性表示函数希望接收的命名参数的个数。</p> <p>prototype属性是保存引用类型的实例方法的真正所在，如toString()和valueOf()。在创建自定义引用类型以及实现继承时prototype极为有用。另外，prototype属性是不可枚举的，即不能用for-in发现。</p> <p>函数<code>apply()</code>和<code>call()</code>，他们的作用都是从指定的作用域中调用函数，除了参数不同。apply接收两个参数：一个是作用域，另一个是参数构成的数组或arguments。call接收多个参数，第一个参数也是作用域，第二个参数及其后面的参数是可选的直接传递给call的参数们。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> color <span class="token operator">=</span> <span class="token string">'red'</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> obj <span class="token operator">=</span> <span class="token punctuation">{</span>color<span class="token operator">:</span><span class="token string">'blue'</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">fn</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
   console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>color<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//red</span>
<span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>window<span class="token punctuation">)</span><span class="token punctuation">;</span>    <span class="token comment">//red</span>
<span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">call</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>   <span class="token comment">//blue</span>
</code></pre></div><p>ES5还定义了一个方法<code>bind()</code>，其this值会绑定到传给bind()的值</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> fn2 <span class="token operator">=</span> <span class="token function">fn</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>obj<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">fn2</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//blue</span>
</code></pre></div><h2 id="基本包装类型"><a href="#基本包装类型" class="header-anchor">#</a> 基本包装类型</h2> <p>ES提供了3个特殊的引用类型（包装类型）：<code>Boolean</code>、<code>Number</code>和<code>String</code>。它门是基本数据类型，每当读取一个基本类型值的时候，后台就会创建一个对应的基本包装类型的对象，从而让我们能够调用一些方法来操作这些数据。基本类型值不是对象，因而从逻辑上讲它们不应该有方法（尽管如我们所愿，它们确实有方法）。</p> <p>使用 new 操作符创建的引用类型的实例，在执行流离开当前作用域之前都一直保存在内存中。而自动创建的基本包装类型的对象，则只存在于一行代码的执行瞬间，然后立即被销毁。</p> <p>不建议直接实例化 Number 类型和 Boolean 类型。</p> <p>Number类型提供了将数值格式化成字符串的方法：</p> <ul><li>toFixed(n)方法会按照指定的小数位(n)返回数值的字符串表示</li> <li>toExponential(n)，该方法返回以指数表示法</li> <li>toPrecision(n)方法可能会返回固定大小（fixed）格式，也可能返回指数（exponential）格式</li></ul> <p>String类型的每个实例都有一个 length 属性，表示字符串中包含多个字符。</p> <p>charAt()和 charCodeAt()方法用于访问字符串中特定字符。charAt()方法以单字符字符串的形式返回给定位置的那个字符（ECMAScript 中没有字符类型）。</p> <p>嘿 <strong>下面介绍常用的裁剪字符串方法：</strong></p> <h3 id="slice"><a href="#slice" class="header-anchor">#</a> slice()</h3> <p>slice()方法接收两个参数，返回从第1个参数值到第2个参数值裁剪的字符串。第1个参数是从0开始，第2个也是从0开始，但第2个参数最后要减1（可以看做从1开始）。slice支持以负数为单位，slice()方法会将传
入的负值与字符串的长度相加。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">&quot;helloworldstring&quot;</span><span class="token punctuation">;</span>
str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;worldstring&quot;</span>
str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment">// &quot;ello&quot;</span>
str<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;strin&quot;</span>
</code></pre></div><h3 id="substring"><a href="#substring" class="header-anchor">#</a> substring()</h3> <p>substring() 与slice主要的不同是，它不支持负数作为参数，substring()方法会把<strong>所有</strong>负值参数都转换为 0。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">&quot;helloworldstring&quot;</span><span class="token punctuation">;</span>
str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;worldstring&quot;</span>
str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment">// &quot;ello&quot;</span>
str<span class="token punctuation">.</span><span class="token function">substring</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;hello&quot;</span>
</code></pre></div><h3 id="substr"><a href="#substr" class="header-anchor">#</a> substr()</h3> <p>substr()的第2个个参数的定义与上述两者不同，它的第二个参数指定的是要返回的字符个数。substr()方法将负的第一个参数加上字符串的长度，而将负的第二个参数转换为0。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> str <span class="token operator">=</span> <span class="token string">&quot;helloworldstring&quot;</span><span class="token punctuation">;</span>
str<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;worldstring&quot;</span>
str<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span><span class="token number">5</span><span class="token punctuation">)</span>  <span class="token comment">// &quot;ellow&quot;</span>
str<span class="token punctuation">.</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">6</span><span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span>    <span class="token comment">// &quot;&quot;</span>
</code></pre></div><p>这三个方法slice()、substr()和 substring()都不会修改字符串本身的值。</p> <h3 id="trim"><a href="#trim" class="header-anchor">#</a> trim()</h3> <p><code>trim()</code>方法会创建一个字符串的副本，删除前置及后缀的所有空格，然后返回结果。</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> stringValue <span class="token operator">=</span> <span class="token string">&quot;   hello world   &quot;</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> trimmedStringValue <span class="token operator">=</span> stringValue<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>stringValue<span class="token punctuation">)</span><span class="token punctuation">;</span>            <span class="token comment">//&quot;   hello world   &quot;</span>
<span class="token function">alert</span><span class="token punctuation">(</span>trimmedStringValue<span class="token punctuation">)</span><span class="token punctuation">;</span>     <span class="token comment">//&quot;hello world&quot;</span>
</code></pre></div><h3 id="字符串大小写转换方法"><a href="#字符串大小写转换方法" class="header-anchor">#</a> 字符串大小写转换方法</h3> <p>toLowerCase()、toLocaleLowerCase()、toUpperCase()和 toLocaleUpperCase()。</p> <h3 id="match"><a href="#match" class="header-anchor">#</a> match()</h3> <p>在字符串上调用match()方法，本质上与调用 RegExp 的 exec()方法相同。match()方法只接受一个参数，要么是一个正则表达式，要么是一个 RegExp 对象。match()方法返回一个数组，数组的第一项是与整个模式匹配的字符串，之后的每一项（如果有）保存着与正则表达式中的捕获组匹配的字符串。</p> <p>search()。这个方法的唯一参数与 match()方法的参数相同：由字
符串或 RegExp 对象指定的一个正则表达式。search()方法返回字符串中第一个匹配项的索引；如果没
有找到匹配项，则返回-1。而且，search()方法始终是从字符串开头向后查找模式。</p> <h3 id="replace"><a href="#replace" class="header-anchor">#</a> replace()</h3> <p>replace()方法接受两个参数：第一个参数可以是一个 <strong>RegExp对象</strong> 或者一个 <strong>字符串</strong>（这个字符串不会被转换成正则表达式），第二个参数可以是一个<strong>字符串</strong>或者一个<strong>函数</strong>。如果第一个参数是字符串，那么只会替换第一个子字符串。要想替换所有子字符串，唯一的办法就是提供一个正则表达式，而且要指定全局（g）标志</p> <p>split()，这个方法可以基于指定的分隔符将一个字符串分割成多个子字符串，并将结果放在一个数组中。</p> <h2 id="单体内置对象"><a href="#单体内置对象" class="header-anchor">#</a> 单体内置对象</h2> <p>意思就是说，开发人员不必显式地实例化内置对象，因为它们已经实例化了。</p> <h3 id="global对象"><a href="#global对象" class="header-anchor">#</a> Global对象</h3> <p>Global这个对象都是“不存在”的，不属于任何其他对象的属性和方法，最终都是它的属性和方法。</p> <p>Global包含很多方法，诸如 isNaN()、isFinite()、parseInt()以及 parseFloat()。除此之外还包含了其他一些方法：</p> <ol><li>URL编码方法：encodeURL()、encodeURIComponent() 和对应的 decodeURI()、decodeURIComponent()。
这些方法对url地址进行编码或解码。</li> <li>eval()方法
eval()方法方法就像是一个完整的 ECMAScript 解析器</li> <li>Global 对象的属性
所有原生引用类型的构造函数，像Object 和 Function，也都是 Global 对象的属性</li> <li>window 对象
在全局作用域中声明的所有变量和函数，就都成为了 window对象的属性。</li></ol> <h3 id="math对象"><a href="#math对象" class="header-anchor">#</a> Math对象</h3> <ol><li>属性值
<ul><li>Math对象提供一些属性值：Math.E、Math.PI。。。</li></ul></li> <li>min()和max()方法
Math.min()和 Math.max()方法用于确定一组数值中的最小值和最大值。</li> <li>舍入方法
<ul><li>Math.ceil()执行向上舍入，即它总是将数值向上舍入为最接近的整数</li> <li>Math.floor()执行向下舍入，即它总是将数值向下舍入为最接近的整数</li> <li>Math.round()执行标准舍入，即它总是将数值四舍五入为最接近的整数</li></ul></li> <li>随机数
<ul><li>Math.random()方法返回大于等于 0 小于 1 的一个随机数。</li> <li>套用下面的公式，就可以利用 Math.random()从某个整数范围内随机选择一个值。</li> <li><code>值 = Math.floor(Math.random() * 可能值的总数 + 第一个可能的值)</code></li></ul></li></ol> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">function</span> <span class="token function">selectFrom</span><span class="token punctuation">(</span><span class="token parameter">lowerValue<span class="token punctuation">,</span> upperValue</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">var</span> choices <span class="token operator">=</span> upperValue <span class="token operator">-</span> lowerValue <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> Math<span class="token punctuation">.</span><span class="token function">floor</span><span class="token punctuation">(</span>Math<span class="token punctuation">.</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">*</span> choices <span class="token operator">+</span> lowerValue<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> num <span class="token operator">=</span> <span class="token function">selectFrom</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 介于 2 和 10 之间（包括 2 和 10）的一个数值</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">上次更新:</span> <span class="time">2023-4-5 17:45:19</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/notes/notes/pro-js-for-web-dev-note/chapter04.html" class="prev">第四章 变量、作用域和内存问题</a></span> <span class="next"><a href="/notes/notes/pro-js-for-web-dev-note/chapter06.html">面向对象的程序设计</a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----><div id="live2d-widget" class="live2d-widget-container" style="position:fixed;right:65px;bottom:0px;width:135px;height:300px;z-index:99999;opacity:0.8;pointer-events:none;"><canvas id="live2d_canvas" width="135" height="300" class="live2d_canvas" style="position:absolute;left:0px;top:0px;width:135px;height:300px;"></canvas></div></div></div>
    <script src="/notes/assets/js/app.f19b5e39.js" defer></script><script src="/notes/assets/js/2.69392339.js" defer></script><script src="/notes/assets/js/45.b89cd059.js" defer></script><script src="/notes/assets/js/14.e376f3f7.js" defer></script>
  </body>
</html>
